package com.hry.store.controller;

import com.hry.store.common.Const;
import com.hry.store.common.ServerResponse;
import com.hry.store.pojo.City;
import com.hry.store.pojo.Distract;
import com.hry.store.pojo.Shipping;
import com.hry.store.pojo.User;
import com.hry.store.service.CityService;
import com.hry.store.service.DistractService;
import com.hry.store.service.ShippingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class ShippingController {
    @Autowired
    private ShippingService shippingService;
    @Autowired
    private CityService cityService;
    @Autowired
    private DistractService distractService;

    /**
     * 添加收货地址
     */
    @RequestMapping("shipping/addShipping")
    @ResponseBody
    public ServerResponse addShipping(HttpServletRequest request,
                              @RequestParam("receiverName") String receiverName,
                              @RequestParam("receiverPhone") Long receiverPhone,
                              @RequestParam("receiverProvince") Integer receiverProvince,
                              @RequestParam("receiverCity") Integer receiverCity,
                              @RequestParam("receiverDistract") Integer receiverDistract,
                              @RequestParam("receiverAddress") String receiverAddress) {

        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(Const.ERROR_CODE, "添加失败");
        }
        int result = shippingService.addNewAddress(user.getUserEmail(), receiverName, receiverPhone, receiverProvince, receiverCity, receiverAddress, receiverDistract);
        if (result > 0) {
            return ServerResponse.createBySuccess("添加成功", Const.SUCCESS_CODE);
        }
        return ServerResponse.createByErrorCodeMessage(Const.ERROR_CODE, "添加失败");
    }

    /**
     * 根据省份查询城市
     */
    @RequestMapping("shipping/getCity")
    @ResponseBody
    public ServerResponse getCity(@RequestParam("provinceId") Integer provinceId) {
        List<City> cityList = cityService.getCity(provinceId);
        if (!CollectionUtils.isEmpty(cityList)) {
            return ServerResponse.createBySuccess(cityList);
        }
        return ServerResponse.createByErrorMessage("查询失败");
    }

    /**
     * 根据城市Id 查询区县
     */
    @RequestMapping("shipping/getDistract")
    @ResponseBody
    public ServerResponse getDistract(@RequestParam("cityId") Integer cityId) {
        List<Distract> distractList = distractService.getByCityId(cityId);
        if (!CollectionUtils.isEmpty(distractList)) {
            return ServerResponse.createBySuccess(distractList);
        }
        return ServerResponse.createByErrorMessage("查询失败");
    }

    /**
     * 根据shippingId查询详情
     */
    @RequestMapping("shipping/getDetail")
    @ResponseBody
    public ServerResponse getDetailByShippingId(@RequestParam("shippingId") Integer shippingId) {
        Shipping shipping = shippingService.getDetailByShippingId(shippingId);
        if (shipping == null) {
            return ServerResponse.createByErrorCodeMessage(Const.ERROR_CODE, "根据shippingId查询收货地址失败");
        }
        return ServerResponse.createBySuccess(shipping);
    }

    /**
     * 修改收货地址
     */
    @RequestMapping("shipping/editShipping")
    @ResponseBody
    public ServerResponse editShipping(HttpServletRequest request,
                                       @RequestParam("shippingId") Integer shippingId,
                                       @RequestParam("receiverName") String receiverName,
                                       @RequestParam("receiverPhone") Long receiverPhone,
                                       @RequestParam("receiverProvince") Integer receiverProvince,
                                       @RequestParam("receiverCity") Integer receiverCity,
                                       @RequestParam("receiverDistract") Integer receiverDistract,
                                       @RequestParam("receiverAddress") String receiverAddress
    ) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        Shipping shipping = new Shipping();
        shipping.setShippingId(shippingId);
        shipping.setUserEmail(user.getUserEmail());
        shipping.setReceiverName(receiverName);
        shipping.setReceiverPhone(receiverPhone);
        shipping.setReceiverProvince(receiverProvince);
        shipping.setReceiverCity(receiverCity);
        shipping.setReveiverDistract(receiverDistract);
        shipping.setReceiverAddress(receiverAddress);
        int result = shippingService.updateShipping(shipping);
        if (result > 0) {
            return ServerResponse.createBySuccess("更新成功", Const.SUCCESS_CODE);

        }
        return ServerResponse.createByErrorCodeMessage(Const.ERROR_CODE, "更新失败");
    }

    /**
     * 删除某个收货地址
     */
    @RequestMapping("shipping/deleteShipping")
    @ResponseBody
    public ServerResponse deleteShipping(@RequestParam("shippingId") Integer shippingId) {
        int resultCount = shippingService.deleteByShippingId(shippingId);
        if (resultCount > 0) {
            return ServerResponse.createBySuccess("删除成功", Const.SUCCESS_CODE);
        }
        return ServerResponse.createByErrorCodeMessage(Const.ERROR_CODE, "删除失败");

    }

}
